package com.handpay.nocardpay.baselibrary.setting.presenter;

import android.content.Context;
import android.text.TextUtils;

import com.handpay.nocardpay.baselibrary.commbase.BaseProtocolKey;
import com.handpay.nocardpay.baselibrary.commbase.model.IBaseModel;
import com.handpay.nocardpay.baselibrary.commbase.model.bean.BaseErrorInfo;
import com.handpay.nocardpay.baselibrary.commbase.model.bean.BaseResponseBean;
import com.handpay.nocardpay.baselibrary.commbase.presenter.IBasePresenter;
import com.handpay.nocardpay.baselibrary.config.SecureManager;
import com.handpay.nocardpay.baselibrary.logutils.HPLog;
import com.handpay.nocardpay.baselibrary.setting.model.bean.ChangeGesturePwdBean;
import com.handpay.nocardpay.baselibrary.setting.model.bean.LoginUserGesturePwdBean;
import com.handpay.nocardpay.baselibrary.setting.model.impl.GestureModelImpl;
import com.handpay.nocardpay.baselibrary.setting.view.IGesturePWDChangeView;
import com.handpay.nocardpay.baselibrary.setting.view.IGesturePWDGetView;
import com.handpay.nocardpay.baselibrary.utils.ClientEngine;
import com.handpay.nocardpay.baselibrary.utils.PreferenceManager;

import java.util.HashMap;

/**
 * Created by lyang on 2017/10/26.
 */

public class GesturePresenterImpl implements IBasePresenter {
    private Context mContext;
    private GestureModelImpl mGestureModelImpl;
    private IGesturePWDGetView mGesturePWDGetView;
    private IGesturePWDChangeView mGesturePWDChangeView;

    public GesturePresenterImpl(Context context) {
        this.mContext = context;
        mGestureModelImpl = new GestureModelImpl(mContext);

    }

    public void setGesturePwdChangeView(IGesturePWDChangeView gesturePWDChangeView) {
        mGesturePWDChangeView = gesturePWDChangeView;
    }


    public void setGesturePwdGetView(IGesturePWDGetView gesturePwdView) {
        mGesturePWDGetView = gesturePwdView;
    }

    public void getLoginUserGesturePwd(HashMap<String, Object> paramers, boolean isLoading) {
        if (mGesturePWDGetView != null) {
            if (isLoading) {
                mGesturePWDGetView.showProgress();
            }
        }
        mGestureModelImpl.getLoginUserGesturePwd(paramers, LoginUserGesturePwdBean.class, new IBaseModel.OnRequestFinishedListener<LoginUserGesturePwdBean>() {
            @Override
            public void onError(BaseErrorInfo baseErrorInfo) {
                if (mGesturePWDGetView != null) {
                    mGesturePWDGetView.hideProgress();
                    mGesturePWDGetView.showErrorAlert(baseErrorInfo);
                }
            }

            @Override
            public void onSuccess(LoginUserGesturePwdBean response) {
                if (mGesturePWDGetView != null) {
                    mGesturePWDGetView.hideProgress();
                    if (!TextUtils.isEmpty(response.getGesturePwd())) {
                        String gesture_pwd = SecureManager.getInstance().des(response.getGesturePwd(), 0, null);
                        HPLog.i(HPLog.YL_TAG, "login gesutre pwd:" + gesture_pwd);
                        ClientEngine.getInstance().saveRMSData(BaseProtocolKey.KEY_GESTURE_PWD, gesture_pwd);
                    }else {
                        ClientEngine.getInstance().removedData(BaseProtocolKey.KEY_GESTURE_PWD);
                        ClientEngine.getInstance().removedData(BaseProtocolKey.KEY_GESTURE_PWD_SWITCH_STATUS);
                    }
                    mGesturePWDGetView.onGesturePWDSuccess();
                }
            }
        });
    }


    public void changeGesturePwd(HashMap<String, Object> paramers, boolean isLoading) {
        if (mGesturePWDChangeView != null) {
            if (isLoading) {
                mGesturePWDChangeView.showProgress();
            }
        }
        mGestureModelImpl.changeGesturePwd(paramers, ChangeGesturePwdBean.class, new IBaseModel.OnRequestFinishedListener<ChangeGesturePwdBean>() {
            @Override
            public void onError(BaseErrorInfo baseErrorInfo) {
                if (mGesturePWDChangeView != null) {
                    mGesturePWDChangeView.hideProgress();
                    mGesturePWDChangeView.showErrorAlert(baseErrorInfo);
                }
            }

            @Override
            public void onSuccess(ChangeGesturePwdBean response) {
                if (mGesturePWDChangeView != null) {
                    mGesturePWDChangeView.hideProgress();
                    if (Integer.valueOf(response.getStatus()) == 1){
                        String gesture_pwd = SecureManager.getInstance().des(response.getGesturePwd(), 0, null);
                        HPLog.i(HPLog.YL_TAG, "login gesutre pwd:" + gesture_pwd);
                        ClientEngine.getInstance().saveRMSData(BaseProtocolKey.KEY_GESTURE_PWD, gesture_pwd);

                        mGesturePWDChangeView.onGesturePWDSuccess();
                    }
                }
            }
        });
    }

    @Override
    public void onDestory() {

    }
}
